const  kafka = require("kafka-node") ; 

const client = new kafka.Client('10.8.8.33:2181/');

const producer = new kafka.HighLevelProducer(client);
producer.on("ready", function() {
    console.log("Kafka Producer is connected and ready.");

    const event = { 
      timestamp: Date.now(),  
      data: "hello"
    };
    
    const buffer = new Buffer.from(JSON.stringify(event));

    // Create a new payload
    const record = [
      {
          topic: "test",
          messages: buffer,
          attributes: 1 /* Use GZip compression for the payload */
      }
    ];
    //Send record to Kafka and log result/error
    producer.send(record, (data) => {
      console.log('callback',data)
    });
});

// For this demo we just log producer errors to the console.
producer.on("error", function(error) {
    console.error(error);
});


// producer.createTopics(['test'], false, function (err, data) {
//   console.log('create topic success! ',data);
// });






// const KafkaService = {
//     sendRecord: ({ type, userId, sessionId, data }, callback = () => {}) => {
//         if (!userId) {
//             return callback(new Error(`A userId must be provided.`));
//         }

//         const event = {
//             id: uuid.v4(),
//             timestamp: Date.now(),
//             userId: userId,
//             sessionId: sessionId,
//             type: type,
//             data: data
//         };

//         const buffer = new Buffer.from(JSON.stringify(event));

//         // Create a new payload
//         const record = [
//             {
//                 topic: "test",
//                 messages: buffer,
//                 attributes: 1 /* Use GZip compression for the payload */
//             }
//         ];

//         //Send record to Kafka and log result/error
//         producer.send(record, callback);
//     }
// };

// export default KafkaService;